package com.joey.user.controller;


import com.joey.common.util.JwtUtil;
import com.joey.common.util.Result;
import com.joey.user.entity.Employee;
import com.joey.user.entity.dto.EmployeeLoginDTO;
import com.joey.user.entity.vo.EmployeeLoginVO;
import com.joey.user.properties.JwtProperties;
import com.joey.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: Joey
 * @Description:
 * @date:2024/6/22 16:22
 */
@RestController
@RequestMapping("/user")
@Slf4j
@Api(tags = "User接口")
@RequiredArgsConstructor
public class UserController {

    @Autowired
    private UserService employeeService;

    private final JwtProperties jwtProperties;

    @PostMapping("/login")
    @ApiOperation("员工登录")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        log.info("登录：{}", employeeLoginDTO);
        Employee employee = employeeService.login(employeeLoginDTO);
        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put("userId", employee.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getUserSecretKey(),
                jwtProperties.getUserTtl(), //设置时间
                claims);                     //todo 设置为null 过期时间用redis检索
        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getName())
                .token(token)
                .build();

        return Result.success(employeeLoginVO);
    }


}
